package com.example.module_word.activity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.blankj.utilcode.util.ToastUtils;
import com.example.module_word.R;
import com.example.module_word.data.DanCiData;
import com.example.module_word.databinding.ActivityPlanBinding;
import com.example.module_word.room.entity.JiHua;
import com.fenghuajueli.libbasecoreui.utils.AdUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class PlanActivity extends AppCompatActivity implements View.OnClickListener {

    private ActivityPlanBinding binding;
    private String kind; //书的种类
    private Long vocabulary; // 词汇量
    private Calendar calendar;
    private int satrtYear,satrtMonth,satrtDay,endYear,endMonth,endDay; //保存当前选中的年、月、日

    private List<JiHua> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityPlanBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        AdUtil.showBannerAd(this,binding.adFrameLayout);
        initView();
        bindListener();
    }

    private void initView() {
        //实例化日历
        calendar = Calendar.getInstance();
        kind = getIntent().getStringExtra("kind");

        list = DanCiData.getJiHuaList(kind);

        vocabulary = DanCiData.getCountByKind(kind);

        if(list.size() == 0){
            satrtYear = calendar.get(Calendar.YEAR);
            satrtMonth = calendar.get(Calendar.MONTH)+1;
            satrtDay = calendar.get(Calendar.DAY_OF_MONTH);
            endYear = calendar.get(Calendar.YEAR);
            endMonth = calendar.get(Calendar.MONTH)+1;
            endDay = calendar.get(Calendar.DAY_OF_MONTH);
            binding.startTimeValue.setText(satrtYear+"."+satrtMonth+"."+satrtDay);
            binding.endTimeValue.setText(endYear+"."+endMonth+"."+endDay);

            int planDay = differentDays(stringToDate(satrtYear+"-"+satrtMonth+"-"+satrtDay),stringToDate(endYear+"-"+endMonth+"-"+endDay))+1;
            binding.planDay.setText("共计学习 "+planDay+" 天");
            //每日新词
            binding.planMrxc.setText((int)(vocabulary/planDay)+"");
            //每日耗时
            binding.planMrhs.setText((int)(vocabulary/planDay)*4+"");

            //获取该书已学习的单词数量
            binding.planProgressBar.setProgress(0);
            binding.planProgressBarValue.setText(0+"");

        }else {
            long startTime = list.get(0).getStart_time();
            calendar.setTime(new Date(startTime));
            satrtYear = calendar.get(Calendar.YEAR);
            satrtMonth = calendar.get(Calendar.MONTH)+1;
            satrtDay = calendar.get(Calendar.DAY_OF_MONTH);
            binding.startTimeValue.setText(satrtYear+"."+satrtMonth+"."+satrtDay);


            long endTime = list.get(0).getEnd_time();
            calendar.setTime(new Date(endTime));
            endYear = calendar.get(Calendar.YEAR);
            endMonth = calendar.get(Calendar.MONTH)+1;
            endDay = calendar.get(Calendar.DAY_OF_MONTH);
            binding.endTimeValue.setText(endYear+"."+endMonth+"."+endDay);

            int planDay = differentDays(stringToDate(satrtYear+"-"+satrtMonth+"-"+satrtDay),stringToDate(endYear+"-"+endMonth+"-"+endDay))+1;
            binding.planDay.setText("共计学习 "+planDay+" 天");
            //每日新词
            binding.planMrxc.setText((int)(vocabulary/planDay)+"");
            //每日耗时
            binding.planMrhs.setText((int)(vocabulary/planDay)*4+"");

            int signDay = list.get(0).getDay();

            int pb = (signDay*100)/planDay;

            binding.planProgressBar.setProgress(pb);
            binding.planProgressBarValue.setText(pb+"");
        }


    }

    private void bindListener() {
        binding.planBack.setOnClickListener(this);
        binding.startTimeValue.setOnClickListener(this);
        binding.endTimeValue.setOnClickListener(this);
        binding.planConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.plan_back){
            //返回
            onBackPressed();
        }else if (id == R.id.start_time_value){
            //开始时间
            showDatePickerDialog(binding.startTimeValue,1);
        }else if (id == R.id.end_time_value){
            //结束时间
            showDatePickerDialog(binding.endTimeValue,2);
        }else if (id == R.id.plan_confirm){
            //确定
            //不存在则新增，存在则修改
            long start = new Date(satrtYear+"/"+satrtMonth+"/"+satrtDay).getTime();
            long end = new Date(endYear+"/"+endMonth+"/"+endDay).getTime();
            if (start>end){
                ToastUtils.showShort("开始时间不可以大于结束时间,请重新设置");
                return;
            }
            if (DanCiData.getJiHuaList(kind).size()==0){
                DanCiData.insertJiHua(new JiHua(kind,new Date(satrtYear+"/"+satrtMonth+"/"+satrtDay).getTime(),
                        new Date(endYear+"/"+endMonth+"/"+endDay).getTime(),vocabulary,0,true));
            }else {
                DanCiData.updateJiHua(kind,new Date(satrtYear+"/"+satrtMonth+"/"+satrtDay).getTime(),
                        new Date(endYear+"/"+endMonth+"/"+endDay).getTime());
            }

            onBackPressed();
        }
    }

    //日期选择器
    public void showDatePickerDialog(TextView tv,int type) {
        //实例化日历
//        Calendar calendar = Calendar.getInstance();
        // 直接创建一个DatePickerDialog对话框实例，并将它显示出来
        DatePickerDialog datePickerDialog =  new DatePickerDialog(PlanActivity.this, R.style.dialog_date,
                // 绑定监听器(How the parent is notified that the date is set.)
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                        if (type==1){
                            satrtYear = year;
                            satrtMonth = monthOfYear+1;
                            satrtDay = dayOfMonth;
                        }else {
                            endYear = year;
                            endMonth = monthOfYear+1;
                            endDay = dayOfMonth;

                            int planDay = differentDays(stringToDate(satrtYear+"-"+satrtMonth+"-"+satrtDay),stringToDate(endYear+"-"+endMonth+"-"+endDay))+1;

                            Log.e("info--",satrtYear+"-"+satrtMonth+"-"+satrtDay+"---"+endYear+"-"+endMonth+"-"+endDay+"---vocabulary:--"+vocabulary);

                            binding.planDay.setText("共计学习 "+planDay+" 天");
                            //每日新词
                            binding.planMrxc.setText((int)(vocabulary/planDay)+"");
                            //每日耗时
                            binding.planMrhs.setText((int)(vocabulary/planDay)*4+"");
                        }
                        tv.setText(year+"."+(monthOfYear+1)+"."+dayOfMonth);


                    }
                }
                // 设置初始日期
                , type==1?satrtYear:endYear
                ,type==1?satrtMonth-1:endMonth-1
                ,type==1?satrtDay:endDay);

        //设置起始日期和结束日期
        DatePicker datePicker = datePickerDialog.getDatePicker();
        //设置起始日期不能在今天之前
        if (type==1){
            datePicker.setMinDate(System.currentTimeMillis());
        }else {
            //结束时间不能在起始时间之前
            datePicker.setMinDate(new Date(satrtYear+"/"+satrtMonth+"/"+satrtDay).getTime());
        }

        datePickerDialog.show();
    }



    private static int differentDays(Date date1,Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2) {//同一年
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1) ;
        } else {// 不同年
            System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }


    private Date stringToDate(String strTime){

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(strTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }
}